#include "dictionarymanagerapi.h"

DictionaryManagerApi::DictionaryManagerApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QByteArray *DictionaryManagerApi::getList(int pageNum, int pageSize, QString dictName,
           QString dictType, QString status, QString beginTime, QString endTime)
{
    QString params = "system/dict/type/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!dictName.isEmpty()){
        params += "&dictName=" + dictName;
    }
    if(!dictType.isEmpty()){
        params += "&dictType=" + dictType;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(beginTime != "2000/1/1" && endTime != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + beginTime + "&params%5BendTime%5D=" + endTime;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::editDict(qlonglong id, DictType &sysDictType)
{
    QString params = "system/dict/type";
    QByteArray arr = QJsonDocument(sysDictType.toJson()).toJson();
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply;
    if(id > 0){
        reply = manager->put(request,arr);
    }else{
        reply = manager->post(request,arr);
    }
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::deleteItems(QString ids)
{
    request.setUrl(baseUrl + "system/dict/type/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::getById(qlonglong id)
{
    request.setUrl(baseUrl + "system/dict/type/" + QString::number(id));
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::refreshCache()
{
    request.setUrl(baseUrl + "system/dict/type/refreshCache");
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::getOptionList()
{
    request.setUrl(baseUrl + "system/dict/type/optionselect");
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::getDictDataList(int pageNum, int pageSize, QString dictType,
                                                  QString status, QString dictLabel)
{
    QString params = "system/dict/data/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!dictType.isEmpty()){
        params += "&dictType=" + dictType;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(!dictLabel.isEmpty()){
        params += "&dictLabel=" + dictLabel;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::editDictData(qlonglong id, DictData &dict)
{
    QString params = "system/dict/data";
    QByteArray arr = QJsonDocument(dict.toJsonObject()).toJson();
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply;
    if(id > 0){
        reply = manager->put(request,arr);
    }else{
        reply = manager->post(request,arr);
    }
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::deleteDictDataItems(QString ids)
{
    request.setUrl(baseUrl + "system/dict/data/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *DictionaryManagerApi::getDictDataById(qlonglong id)
{
    request.setUrl(baseUrl + "system/dict/data/" + QString::number(id));
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}
